<?php
namespace app\houtai\controller\system;
use app\houtai\controller\BaseController;
use think\db\Expression;

class CashController extends BaseController {
  /**
   *  @brief  : 系统管理-提现设置
   *  @return : page
   *  @details: 
   */
  public function configAction() {
    if ($this->request->isGet()) {
      $list = table('WxGzh', 'rydb.websms')->field([
        'ID', 'AppName', 'Remark', 'UpdateTime', 'CashCase', 'CashPool',
        'CashMin', 'WarnMin', 'WarnTel',
      ])->where('Nullity', 0)->select();
      return $this->fetch('', ['list' => $list]);
    } else {
      $data = $this->request->param('data/a', []);
      if (!empty($data)) {
        foreach($data as $row) {
          $ID = $row['ID'] ?? 0;
          $CashCase = $row['CashCase'] ?? 0;
          $CashMin  = $row['CashMin'] ?? 0;
          $CashPool = $row['CashPool'] ?? 0;
          $WarnMin  = $row['WarnMin'] ?? 0;
          $WarnTel  = $row['WarnTel'] ?? '';
          $Remark   = $row['Remark'] ?? '';
          if ($ID > 0) {
            table('WxGzh', 'rydb.websms')->where('ID', $ID)->update([
              'CashCase'  => $CashCase,
              'CashMin'   => $CashMin,
              'CashPool'  => $CashPool,
              'WarnMin'   => $WarnMin,
              'WarnTel'   => $WarnTel,
              'Remark'    => $Remark,
              'UpdateTime'=> new Expression('GETDATE()'),
            ]);
          }
        }
      }
      return result([], 0);
    }
  }

  /**
   * @brief : 短信配置
   * @detail ：list
   * @data ： array
   * */
    public function  SmsconfAction(){
        $list = table('SMSConfig','rydb.websms')->paginate();
        return $this->fetch('', ['list' => $list]);
    }

    /**
     * @brief : 短信配置详情
     * @detail : ajax修改
     * @data :json
     **/

    public function SmsdetailAction(){
        $id = $this->request->param('id/d', 0);
        $users = table('SMSConfig', 'rydb.websms')->where(['ID' => $id])->find();
        if ($this->request->isGet()) {
            return $this->fetch('', ['row' => $users ?? ['ID'=>0]]);
        } else if($this->request->isPost()){
            $data=[
                'Users'=>$this->request->param('Users/s', ''),
                'Pwd'=>$this->request->param('Pwd/s',''),
                'Status'=>$this->request->param('Status/d',0),
            ];
            $RMB = $this->checkRMB($data['Users'],$data['Pwd']);
            if (empty($users)) {
                //新建
                if ($data['Users'] == '' && $data['Pwd'] == '')   {
                    return result([], 100, '请填写有效的账户和密码');
                }else{
                    if ($RMB < 0 ){
                        return result([],101,'账户参数错误');
                    }else{
                        $data['BalanceRMB'] = $RMB;
                        $eax =  table('SMSConfig', 'rydb.websms')->insert($data);
                        return result([], 0,'创建成功');
                    }

                }

            }

            $data['CreateTime']   = new Expression('GETDATE()');    //修改时间
            //修改
            $eax = table('SMSConfig', 'rydb.websms')->where('ID', $id)->update($data);

            return result([], 0);
        }


    }
    /**
     * @brief : 短信余额获取
     * @detail : curl
     * @data : BalanceRMB
     **/
    protected  function checkRMB($users,$pwd){
        $url = 'http://m.isms360.com:8085/querybalance.ashx';
        $data = [
            'User'=>$users,
            'Pwd' =>$pwd
        ];
        $ch = curl_init();
        trace($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url);
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
        $return = curl_exec ( $ch ); //$return  返回结果，如果是以 “-” 开头的为发送失败，请查看错误代码，否则为MSGID
        curl_close ( $ch );
        return $return;
        trace($return);
    }

    /**
     * @brief : 短信配置删除
     * @detail : ajax删除
     * @data : code
     **/
    public function SmsdelAction(){
        $id = $this->request->param('id/d', 0);
        table('SMSConfig','rydb.websms')->where('ID',$id)->delete();
        return result([],0);

    }
}
